<?php
/**
 * 《计费管理》实现类
 *
 * @author 孙晓晔
 * @version $Id: Mold.class.php 1053 2010-04-07 11:15:15Z sunxy $
 */
require_once(dirname(__FILE__) . '/TelUtil.class.php');

class Charge extends TelUtil {
	var $office = 'ns';

	function __construct() {
		$request['update'] = '$Date: 2009/02/16 07:36:07 $';
		$request['revision'] = '$Revision: 1.1 $';

		parent::__construct($request);
	}
	
	var $r = array('s'=>'');
	
	function execute($s, $d, $t) {
		$this->r['s'] = $s;
		$this->r['d'] = $d;
		$this->r['t'] = $t;
		
		if (preg_match("/^(6666)$/", $s)) {
			$this->r['c'] = 0;
			return 2;
		}
			
		if ($this->_inner($s)) {
			$this->r['c'] = 0;
			return 1;
		}
		
		if (preg_match("/^6\d{4,5}$/", $s)) {
			$this->r['c'] = 0;
			return 2;
		}
		
		if (preg_match("/^9/", $s)) {
			$this->r['c'] = $this->_ct(substr($s, 1), $d, $t);
			return 3;
		}
		
		if (preg_match("/^(194|195)/", $s)) {
			$this->r['c'] = $this->_ct(substr($s, 3), $d, $t);
			return 3;
		}
		
		if (preg_match("/^(196)/", $s)) {
			$this->r['c'] = $this->_cm(substr($s, 3), $d, $t);
			return 3;
		}
		
		// $this->r['c'] = $this->_ip($s, $d, $t);
		return 4;
	}
	
	function _cost($c, $s, $d, $t) {
		if (preg_match("/^(DAHDI|Zap|Local)/i", $s)) {
			return 0;
		}
		
		// echo sprintf("s = %s, d = %s, t = %s\n", $s, $d, $t);
		
		$this->execute($s, $d, $t);
		
		return $this->r['c'];
	}
	
	function _inner($s) {
		if (preg_match("/^4\d{1,6}$/", $s)) {
			return true;
		}
	
		if (preg_match("/^\d{1,4}$/", $s)) {
			return true;
		}
	
		if (preg_match("/^(2|6|8)\d{3}$/", $s)) {
			return true;
		}
	
		return false;
	}
	
	function _ct($s, $d, $t) {
		if ($this->office == 'qx') {
			return $this->_cm($s, $d, $t);
		}
		
		if (preg_match("/^11808/", $s)) {
			return $this->_ct_11808(substr($s, 5), $d, $t);
		}
		
		if (preg_match("/^96688/", $s)) {
			return $this->_ct_96688(substr($s, 5), $d, $t);
		}
		
		if (preg_match("/^17909/", $s)) {
			return $this->_ct_17909(substr($s, 5), $d, $t);
		}
		
		return $this->_ct_local($s, $d, $t);
	}
	
	function _ct_11808($s, $d, $t) {
		
		if (preg_match("/^0/", $s)) {
			if (preg_match("/^00/", $s)) {
				if (preg_match("/^00(852|1|65|66)/", $s)) {
					return $this->_ct_11808_a($s, $d, $t);
				}
				
				if (preg_match("/^00(82|60|853|81|)/", $s)) {
					return $this->_ct_11808_b($s, $d, $t);
				}
				
				if (preg_match("/^00(852|1|65|66)/", $s)) {
					return $this->_ct_11808_c($s, $d, $t);
				}
			}
			
			return $this->_ct_11808_a($s, $d, $t);
		}
		
	}
	
	function _ct_11808_a($s, $d, $t) {
		
		if ($d > 600) {
			return 2.9 + ceil(($d - 600) / 60) * 0.19;
		}
			
		return ceil($d / 60) * 0.29;
	}
	
	function _ct_11808_b($s, $d, $t) {
		if ($d > 600) {
			return 7.9 + ceil(($d - 600) / 60) * 0.39;
		}
			
		return ceil($d / 60) * 0.79;
	}
	
	function _ct_11808_c($s, $d, $t) {
			
		return ceil($d / 60) * 0.79;
	}
	
	function _ct_96688($s, $d, $t) {
	
		if (preg_match("/^0/", $s)) {
			if (preg_match("/^00/", $s)) {
				if (preg_match("/^00(852)/", $s)) {
					return $this->_ct_96688_a($s, $d, $t);
				}
		
				if (preg_match("/^00(82|60|853|81|)/", $s)) {
					return $this->_ct_96688_b($s, $d, $t);
				}
		
				if (preg_match("/^00(852|1|65|66)/", $s)) {
					return $this->_ct_96688_c($s, $d, $t);
				}
				
				if (preg_match("/^00(852|1|65|66)/", $s)) {
					return $this->_ct_96688_d($s, $d, $t);
				}
				
				if (preg_match("/^00(852|1|65|66)/", $s)) {
					return $this->_ct_96688_e($s, $d, $t);
				}
				
				if (preg_match("/^00(852|1|65|66)/", $s)) {
					return $this->_ct_96688_f($s, $d, $t);
				}
				
				return $this->_ct_96688_g($s, $d, $t);
			}
				
			return $this->_ct_96688_a($s, $d, $t);
		}
	}
	
	function _leisure($t) {
		$tt = strtotime($t);
		$h = date("G", $tt);
		if ($h >= 20) {
			return true;
		}
	
		if ($h < 8) {
			return true;
		}
	
		$w = date("w", $tt);
		if ($w == 6 || $w == 0) {
			return true;
		}
	
		return false;
	}
	
	function _ct_96688_a($s, $d, $t) {
		$u = 0.1;
		if ($this->_leisure($t)) {
			$u = 0.03;
		}
		
		return ceil($d / 6) * $u;
	}
	
	function _ct_96688_b($s, $d, $t) {
		$u = 0.1;
		if ($this->_leisure($t)) {
			$u = 0.05;
		}
			
		return ceil($d / 6) * $u;
	}
	
	function _ct_96688_c($s, $d, $t) {
		$u = 0.16;
		if ($this->_leisure($t)) {
			$u = 0.08;
		}
			
		return ceil($d / 6) * $u;
	}
	
	function _ct_96688_d($s, $d, $t) {
		$u = 0.15;
		if ($this->_leisure($t)) {
			$u = 0.09;
		}
			
		return ceil($d / 6) * $u;
	}
	
	function _ct_96688_e($s, $d, $t) {
		$u = 0.32;
		if ($this->_leisure($t)) {
			$u = 0.16;
		}
			
		return ceil($d / 6) * $u;
	}
	
	function _ct_96688_f($s, $d, $t) {
		$u = 0.36;
		if ($this->_leisure($t)) {
			$u = 0.28;
		}
			
		return ceil($d / 6) * $u;
	}
	
	function _ct_96688_g($s, $d, $t) {
		$u = 0.78;
			
		return ceil($d / 6) * $u;
	}
	
	function _ct_17909($s, $d) {
		$u = 2.4;
			
		return ceil($d / 60) * $u;
	}
	
	function _ct_local($s, $d) {
		if (preg_match("/^1(3|4|5|8)\d{9}$/", $s)) {
			$u = 0.2;
				
			return ceil($d / 60) * $u;
		}
		
		if ($d > 180) {
			return 0.22 + ceil(($d - 180) / 60) * 0.11;
		}
			
		return 0.22;
	}
	
	function _cm($s, $d, $t) {
		if (preg_match("/^0/", $s)) {
			if (preg_match("/^00/", $s)) {
				
				if (preg_match("/^00(852|853)/", $s)) {
					$u = 1.5;
					
					return ceil($d / 60) * $u;
				}
				
				if (preg_match("/^00(1)/", $s)) {
					$u = 2.4;
					
					return ceil($d / 60) * $u;
				}
				
				$u = 3.2;
					
				return ceil($d / 60) * $u;
			}
			
			$u = 0.3;
				
			return ceil($d / 60) * $u;
		}
		
		return $this->_cm_local($s, $d);
	}
	
	function _cm_local($s, $d) {
		if (preg_match("/^6006\d{4,5}$/", $s)) {
			return 0;
		}
		
		if (preg_match("/^1(3|4|5|8)\d{9}$/", $s)) {
			$u = 0.18;
	
			return ceil($d / 60) * $u;
		}
	
		if ($d > 180) {
			return 0.20 + ceil(($d - 180) / 60) * 0.1;
		}
			
		return 0.20;
	}
	
	function t($controller, $request) {
		
		$s = '6026';
		$s = '9118080085289809672';
		$s = '9966880085289809672';
		$s = '9179090085289809672';
		$s = '911808079189809672';
		$s = '989809672';
		$s = '19613510129026';
		$s = '196079139012955';
		
		$d = 150;
		$t = "2014-01-20 15:00:00";
		
		$this->execute($s, $d, $t);
		
		ksort($this->r);
		
		print_r($this->r);
	
		logdate($controller, $request);
		return true;
	}
	
	function z($controller, $request) {
	
		$t = "2014-01-20 08:00:00";
		
		if ($this->_leisure($t)) {
			echo "111";
		} else {
			echo "222";
		}
	
		logdate($controller, $request);
		return true;
	}
}
?>